<?php

/**

 * tpshop

 * ============================================================================

 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。

 * 网站地址: http://www.tp-shop.cn

 * ----------------------------------------------------------------------------

 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .

 * 不允许对程序代码以任何形式任何目的的再发布。

 * ============================================================================

 * $Author: IT宇宙人 2015-08-10 $

 */

namespace app\mobile\controller;

use app\home\logic\UsersLogic;
use app\home\logic\WechatLogic;
use think\Controller;
use think\Session;
use think\Db;



class MobileBase2 extends Controller {

    public $session_id;

    public $dltoken;

    public $weixin_config=array();

    public $cateTrre = array();

    /*

     * 初始化操作

     */

    public function _initialize() {

        Session::start();

        header("Cache-control: private");  // history.back返回后输入框值丢失问题 

        $this->session_id = session_id(); // 当前的 session_id

        define('SESSION_ID',$this->session_id); //将当前的session_id保存为常量，供其它方法调用
        
        // 判断当前用户是否手机                

        if(isMobile())

            cookie('is_mobile','1',3600); 

        else 

            cookie('is_mobile','0',3600);


        //获取鼎联token
$_SESSION['dltoken']=null;
       if($_SESSION['dltoken']){
            $this->dltoken=$_SESSION['dltoken'];
       }else{
           $this->dltoken=I('get.dltoken');

           $_SESSION['dltoken']=$this->dltoken;

       }

       if(!$_SESSION['dltoken']&&strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){

           $this->error('参数缺失');

       }
       $this->weixin_config= M('wx_user')->where(array('appid'=>$this->dltoken))->find(); //获取当前商户微信配置

        /*更新缓存中用户信息*/
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            if(!$user['dlopenid']){
                $Wechat = new WechatLogic();
                $dlconfig= M('wx_user')->where(array('appid'=>'wx0c30f2ddb510d331'))->cache(true)->find();
                $Wechat->set_config($dlconfig);
                $dlopenid= $Wechat->getOpenId();
                M('users')->where("user_id", $user['user_id'])->save(array('dlopenid'=>$dlopenid));
                $user['dlopenid']=$dlopenid;
            }
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
        }

       

        //微信浏览器

       //session('issubscribe',null);

        if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') && empty($this->user['openid'])){

            $this->assign('wechat_config', $this->weixin_config); 

            if(is_array($this->weixin_config) && $this->weixin_config['wait_access'] == 1){

                //$wxuser = $this->GetOpenid(); //授权获取openid以及微信用户信息

                //微信自动登录                             
                $Wechat = new WechatLogic();
                $Wechat->set_config($this->weixin_config);
                $openid= $Wechat->getOpenId();
                

                $user = M('users')->where("openid", $openid)->find();
                if($user){
                    $this->user = $user;
                }else{
                    $userinfo= $Wechat->getUserInfo($openid);
                    $userinfo['appid'] = $this->weixin_config['appid'];
                    $logic = new UsersLogic();
                    $user_id=$logic->save_userdata($userinfo); 
                    var_dump($user_id);
                    $this->user=M('users')->where("user_id", $user_id)->find();
                }
                
                if($this->user){

                    session('user',$this->user);

                    setcookie('user_id',$this->user['user_id'],null,'/');

                    setcookie('is_distribut',$this->user['is_distribut'],null,'/');

                    setcookie('uname',$this->user['nickname'],null,'/');                    

                    // 登录后将购物车的商品的 user_id 改为当前登录的id

                    M('cart')->where("session_id", $this->session_id)->save(array('user_id'=>$this->user['user_id']));

                    $cartLogic = new \app\home\logic\CartLogic();

                    $cartLogic->login_cart_handle($this->session_id,$this->user['user_id']);  //用户登录后 需要对购物车 一些操作
                    $this->user_id = $this->user['user_id'];

                    

                }

            }



        }

        $_SESSION['signPackage']=null;

        if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') && empty($_SESSION['signPackage'])){
          // 微信Jssdk 操作类 用分享朋友圈 JS            
               $jssdk = new \app\mobile\logic\Jssdk($this->weixin_config['appid'], $this->weixin_config['appsecret']);
               $signPackage = $jssdk->GetSignPackage();
               $_SESSION['signPackage']=$signPackage;   
        }

        $this->public_assign();

    }

    

    /**

     * 保存公告变量到 smarty中 比如 导航 

     */   

    public function public_assign()

    {

       $tpshop_config = array();

       $tp_config = M('config')->cache(true,TPSHOP_CACHE_TIME)->select();       

       foreach($tp_config as $k => $v)

       {

       	  if($v['name'] == 'hot_keywords'){

       	  	 $tpshop_config['hot_keywords'] = explode('|', $v['value']);

       	  }       	  

          $tpshop_config[$v['inc_type'].'_'.$v['name']] = $v['value'];

       }                        

       

        $goods_category_tree = get_goods_category_tree();//获取商品分类
        $shop_category_tree = get_shop_category_tree();//获取店铺商品分类
        //var_dump($shop_category_tree);

       $this->cateTrre = $goods_category_tree;

       $this->assign('goods_category_tree', $goods_category_tree);   
       $this->assign('shop_category_tree', $shop_category_tree);                    

       $brand_list = M('brand')->cache(true,TPSHOP_CACHE_TIME)->field('id,cat_id,logo,is_hot')->where("cat_id>0")->select();              

       $this->assign('brand_list', $brand_list);

       $this->assign('tpshop_config', $tpshop_config);

    }      



    

    public function ajaxReturn($data){

        exit(json_encode($data));

    }



}